/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package com.sdd.facades.business;

import com.sdd.entities.Comment;
import com.sdd.entities.Product;
import com.sdd.facades.persistence.CommentPersistenceFacade;
import com.sdd.plains.CommentPlain;
import com.sdd.plains.ProductPlain;
import java.util.Date;
import java.util.List;
import javax.ejb.EJB;
import javax.ejb.Stateless;
import javax.ejb.LocalBean;

/**
 *
 * @author Carlos
 */
@Stateless
@LocalBean
public class CommentBusinessFacadeBean {

    @EJB
    CommentPersistenceFacade commentPersistenceFacade;
    @EJB
    ProductBusinessFacadeBean productPersistenceFacade;
    @EJB
    CustomerBusinessFacadeBean customerPersistenceFacade;
    
    public Comment addComment(CommentPlain plain) {
        Comment c = new Comment();
        c.setCommentator(customerPersistenceFacade.getCustomer(plain.getCommentator().getId()));
        c.setProduct(productPersistenceFacade.getProduct(plain.getProduct().getProductId()));
        c.setText(plain.getText());
        c.setDateAdded(new Date());
        return commentPersistenceFacade.create(c);
    }
    
    public Comment getComment(Long id){
        return commentPersistenceFacade.find(id);
    }
    
    public List<Comment> getCommentsByProduct(ProductPlain productPlain){
        Product p = productPersistenceFacade.getProduct(productPlain.getProductId());
        return commentPersistenceFacade.getCommentsByProduct(p);
    }
    
}
